package com.lwp.demo.js.translation;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;

/**
 * 翻译js的文件
 */
public class JavaScriptTranslationProcessorTest implements TranslationProcessor {

    private String source;

    private boolean sourceIsFile;

    private final String JS_EXT_NAME = "js";

    private final String DEF_ENCODING = "UTF-8";

    private String encoding = DEF_ENCODING;

    /**
     *
     * @param source 可以传入文件或者目录
     */
    public JavaScriptTranslationProcessorTest(String source) {
        this.source = source;
        Objects.nonNull(source);
        File file = new File(source);
        if (!file.exists()) {
            throw new RuntimeException("您传入的不是一个文件或目录");
        }

        if (file.isFile()) {
            sourceIsFile = true;
        }
    }
    /**
     *
     * @param source 可以传入文件或者目录
     */
    public JavaScriptTranslationProcessorTest(String source, String encoding) {
        this(source);
        this.encoding = encoding;
    }

    @Override
    public void translation() {

        File file = new File(source);
        if (sourceIsFile) {
            translationJsFile(file);
        } else {
            // 是一个目录
        }
    }

    private String translationJsFile(File file) {
        StringBuilder sb = new StringBuilder(256);

        // 1. 判断是否是js文件
        String fileName = file.getName();
        if (!StringUtils.endsWith(fileName, JS_EXT_NAME)) {
            // js文件才处理
            return "";
        }

        List<String> resultList = new ArrayList();

        // 2. 读取文件内容
        try {

            List<String> readLines = IOUtils.readLines(new FileInputStream(file), encoding);
            if (readLines == null || readLines.isEmpty()) {
                return "";
            }
            String line = "";
            for (int i = 0; i < readLines.size(); i++) {
                line = readLines.get(i);
                System.out.println(line);

                // 注释内容处理
                // 单行注释处理
                if (line.trim().startsWith("//")) {
                    // 单行注释,整行是注释
                    resultList.add(line);
                    continue;
                }
                if (StringUtils.contains(line, "//")) {
                    String[] split1 = StringUtils.split(line, "//", 2);
//                    resultList.add(line);
                    String content = split1[0]; // 有效的js内容
                    if (split1.length > 1) {
                        if (RegUtils.containChinese(content)) {
                            System.out.println("123123" + content);
                        } else {
                            // 不包含中文
                            resultList.add(content);
                        }
                        resultList.add("//" + split1[1]);
                    } else {
                        resultList.add(line);
                    }
                    continue;
                }

                // 扫描中文
                if (RegUtils.containChinese(line)) {
//                    System.out.println("9999999: " + line); // 打印包含中文的中文行
                    Matcher matcher = RegUtils.CHINESE_SINGLE.matcher(line);
                    // 匹配单引号中文
                    while (matcher.find()) {
                        String group = matcher.group();
                        if (RegUtils.containChinese(group)) {
                            System.out.println(group);
                        }
                    }
                    Matcher doubleMatcher = RegUtils.CHINESE_DOUBLE.matcher(line);
                    // 匹配双引号中文
                    while (doubleMatcher.find()) {
                        String group = doubleMatcher.group();
                        if (RegUtils.containChinese(group)) {
                            System.out.println(group);
                        }
                    }
                } else {
                    // 不包含中文
                    resultList.add(line);
                }
                resultList.add(line);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (resultList == null || resultList.isEmpty()) {
            return "";
        }
        for (int i = 0; i < resultList.size(); i++) {
            if (i != 0) {
                sb.append(System.lineSeparator());
            }
            sb.append(resultList.get(i));
        }
        return sb.toString();
    }
}
